<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-model-item label="编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入编号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="客户分类名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="custName">
              <j-popup
                v-model="model.custName"
                field="custName"
                org-fields="id,name"
                dest-fields="custCode,custName"
                code="erp_materialclass_ref"
                :multi="false"

              />

            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="助记码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mnemonicCode">
              <a-input v-model="model.mnemonicCode" placeholder="请输入助记码" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="手机号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入手机号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="电子邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="email">
              <a-input v-model="model.email" placeholder="请输入电子邮箱" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="邮编" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zipCode">
              <a-input v-model="model.zipCode" placeholder="请输入邮编" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="传真" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fax">
              <a-input v-model="model.fax" placeholder="请输入传真" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" placeholder="请输入地址" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="结算方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="settleType">
              <j-dict-select-tag type="list" v-model="model.settleType" dictCode="erp_settle" placeholder="请选择结算方式" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="统一社会信用代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="creditCode">
              <a-input v-model="model.creditCode" placeholder="请输入统一社会信用代码" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="纳税人识别号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taxIdentifyNo">
              <a-input v-model="model.taxIdentifyNo" placeholder="请输入纳税人识别号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="开户银行" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bankName">
              <a-input v-model="model.bankName" placeholder="请输入开户银行" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="户名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="accountName">
              <a-input v-model="model.accountName" placeholder="请输入户名" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="银行账号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="accountNo">
              <a-input v-model="model.accountNo" placeholder="请输入银行账号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="erp_status" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="客户联系人子表" :key="refKeys[0]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[0]"
          :loading="erpCustomerLinkTable.loading"
          :columns="erpCustomerLinkTable.columns"
          :dataSource="erpCustomerLinkTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          :customInputHandle='true'
          @valueChange='valueChange'
          />
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction,putAction } from '@/api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import { duplicateCheck } from '@api/api'
  const ruleBaseURL = '/sys/fillRule/executeRuleByCode/'

  export default {
    name: 'ErpCustomerForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        model:{
         },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
           code: [
              { required: true, message: '请输入编号!'},
           ],
           name: [
              { required: true, message: '请输入名称!'},
           ],
           mnemonicCode: [
              { required: true, message: '请输入助记码!'},
           ],
           settleType: [
              { required: true, message: '请输入结算方式!'},
           ],
           status: [
              { required: true, message: '请输入状态!'},
           ],
          phone: [
            { required: true, message: '请输入手机号!'}
          ],
        },
        refKeys: ['erpCustomerLink', ],
        tableKeys:['erpCustomerLink', ],
        activeKey: 'erpCustomerLink',
        // 客户联系人子表
        erpCustomerLinkTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '联系人',
              key: 'name',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '联系人电话',
              key: 'phone',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '邮箱',
              key: 'email',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          rule: {
          // 编码规则校验 填写自己的编码规则
            orderCode: ruleBaseURL + 'ces_order_rule'
          },
          add: "/base/erpCustomer/add",
          edit: "/base/erpCustomer/edit",
          queryById: "/base/erpCustomer/queryById",
          erpCustomerLink: {
            list: '/base/erpCustomer/queryErpCustomerLinkByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      getOrderCode() {
        putAction(this.url.rule.orderCode, this.model).then(res => {
          // 执行成功，获取返回的订单编号值，并赋到页面上
          if (res.success) {
            //赋值给自己定义的订单编号
            this.model.orderCode = res.result
          }
        })
      },
      addBefore(){
      this.getOrderCode();
        this.erpCustomerLinkTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.erpCustomerLink.list, params, this.erpCustomerLinkTable)
        }
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          erpCustomerLinkList: allValues.tablesValue[0].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
      validatePhone(rule, value, callback){
        if(!value){
          callback()
        }else{
          if(new RegExp(/^1[3|4|5|7|8|9][0-9]\d{8}$/).test(value)){
            // var params = {
            //   tableName: 'erp_customer',
            //   fieldName: 'phone',
            //   fieldVal: value,
            //   // dataId: this.model.id
            // };
            // duplicateCheck(params).then((res) => {
            //   if (res.success) {
            //     callback()
            //   } else {
            //     callback("手机号已存在!")
            //   }
            // })
          }else{
            callback("请输入正确格式的手机号码!");
          }
        }
      },
      valueChange(value){
        if (value.col.key === 'phone'){
          if (this.model.phone && value.value=== this.model.phone){
            this.$message.error("与表头手机号重复,请重新填写手机号")
          }
        }
      }

    }
  }
</script>

<style scoped>
</style>
